#!  python
# -*- coding: UTF-8 -*-
'''
@Author ：Star
@Date ：2022/9/30  
'''
import difflib

from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep

op = webdriver.ChromeOptions()
op.add_experimental_option("excludeSwitches", ["enable-automation"])
op.add_experimental_option("useAutomationExtension", False)
driver = webdriver.Chrome(options=op)

driver.get("http://news.baidu.com")
newList = driver.find_elements(By.CSS_SELECTOR, "div#localnews-zixun> ul > li")
for i in newList:
    content = i.text
    i.click()

    all_handles = driver.window_handles
    driver.switch_to.window(all_handles[1])
    sleep(2)

    title = driver.title  #截取前15个字
    if len(content) > 15:
        content = content[0:15]
    if len(title) > 15:
        title = title[0:15]
    rate = difflib.SequenceMatcher(None, content, title).quick_ratio()
    # print("相识度", rate)
    # print("con", content)
    # print("tit", title)
    assert rate > 0.9   #相识度大于90%  即为成功
    driver.close()
    sleep(2)
    driver.switch_to.window(all_handles[0])
    sleep(2)
print("success")
driver.quit()
